package model;

import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;

import java.math.BigDecimal;
import java.util.List;

public class Product {
	@ManagedProperty(value = "#{param.id}")
	private String id;

	private String name;

	private float price;

	private String catName;

	private float weight;

	private float length;

	private float height;

	private boolean isPresent;

	private boolean foamed;

	private int count;

	private String imgPath;

	private String videoUrl;

	private String description;

	private List<Gallery> galleries;

	public Product() {
	}

	public float getLength() {
		return length;
	}

	public void setLength(float length) {
		this.length = length;
	}

	public float getHeight() {
		return height;
	}

	public void setHeight(float height) {
		this.height = height;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public BigDecimal getTotalPrice() {

		HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext
				.getCurrentInstance().getExternalContext().getResponse();

		BigDecimal subTotal = new BigDecimal(this.price * this.count).setScale(
				1, BigDecimal.ROUND_HALF_DOWN);

		Cookie cookie = new Cookie("cart_total", String.valueOf(subTotal));
		cookie.setPath("/");
		cookie.setMaxAge(24 * 3600 * 14);
		httpServletResponse.addCookie(cookie);

		return subTotal;
	}

	public List<Gallery> getGalleries() {
		return galleries;
	}

	public void setGalleries(List<Gallery> galleries) {
		this.galleries = galleries;
	}

	public String getDescription() {

		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getImgPath() {
		return imgPath;
	}

	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public boolean isPresent() {
		return isPresent;
	}

	public void setPresent(boolean isPresent) {
		this.isPresent = isPresent;
	}

	public float getWeight() {
		return weight;
	}

	public void setWeight(float weight) {
		this.weight = weight;
	}

	public String getCatName() {
		return catName;
	}

	public void setCatName(String catName) {
		this.catName = catName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public boolean isFoamed() {
		return foamed;
	}

	public void setFoamed(boolean foamed) {
		this.foamed = foamed;
	}

	public String getVideoUrl() {
		return videoUrl;
	}

	public void setVideoUrl(String videoUrl) {
		this.videoUrl = videoUrl;
	}

	public boolean equals(Object o) {
		final Product other = (Product) o;

		if (this.name.equals(other.getName())) {
			return true;
		}

		return false;
	}

	@Override
	public int hashCode() {
		// hash code is product name oriented as far names are unique
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + name.hashCode();
		return result;
	}

	// public boolean equals(Object o) {
	// return EqualsBuilder.reflectionEquals(this, o);
	// }
	//
	// public int hashcode() {
	// return HashCodeBuilder.reflectionHashCode(this);
	// }

}
